﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;

using YT.DB;
using YT.Commons;
using YT.Modules;

namespace YT.XH.NOSE
{
    public partial class frmNSDrTreatHistoryDetail : Form
    {
        private string mstrSickRecID = string.Empty;
        private string mstrSeqNo = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        private string mstrTableName = "NoseDrTreatHistoryDtl";
        private FormStatus mfrmStatus = new FormStatus();
        private bool mblnExit = false;
        private bool mblnPressNextVist = false;
        private frmNSCDReturnVisitDetail frmVisit = null;

        public frmNSDrTreatHistoryDetail(string strSeqNo, string strSickRecID)
        {
            InitializeComponent();
            mstrSeqNo = strSeqNo;
            mstrSickRecID = strSickRecID;
            if (strSeqNo == "" || strSeqNo == "0")
                mfrmStatus = FormStatus.AddNew;
            else
                mfrmStatus = FormStatus.Update;
        }

        private void FillVisit()
        {            
            string strSQL = string.Empty;
            DataRow drRow = null;
            string strSeqNo = string.Empty;
            
            if (mfrmStatus == FormStatus.Update)
            {
                strSQL = "select top 1 * from " + mstrTableName + " where SeqNo='" + mstrSeqNo + "' and SickRecID='"+ mstrSickRecID +"'";
                drRow = mdbcon.GetDataFirstRow(strSQL);
                strSeqNo = CvtFunc.ToString(drRow["SeqNo"]);
            }
            else if (mfrmStatus == FormStatus.AddNew)
            {
                drRow = mdbcon.GetDataTable("select top 0 * from " + mstrTableName + " ").NewRow();
                strSeqNo = (CvtFunc.ToInt(DataFunctions.GetFieldValue(mstrTableName, "max(SeqNo) as SeqNo", " 1=1 and SickRecID='"+ mstrSickRecID +"' ")) + 1).ToString();
            }

            if (frmVisit != null) frmVisit.Close();
            frmVisit = new frmNSCDReturnVisitDetail(strSeqNo, mstrSickRecID);
            frmVisit.TopLevel = false;
            if (pnlVisit.Controls.Count == 0) this.pnlVisit.Controls.Add(frmVisit);
            frmVisit.Location = new Point(0, 0);
            frmVisit.Dock = DockStyle.Fill;
            frmVisit.ShowVist(); 
            frmVisit.Size = new Size(this.pnlVisit.Width, this.pnlVisit.Height);
            frmVisit.Visible = true;
        }
       

        private void initContext()
        {
            this.Text = "鼻炎病人医生记录资料";
            string strSQL = string.Empty;
            DataRow drRow = null;
            strSQL ="select * from mSickPerson where SickRecID='"+ mstrSickRecID  +"'";
            drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                txtSickName.Text = CvtFunc.ToString ( drRow["SickName"]);
                txtPhone.Text = CvtFunc.ToString(drRow["Phone"]) + "  " + CvtFunc.ToString(drRow["MobilePhone"]);

                strSQL = "select top 1 * from titleResult where SickRecID='"+ mstrSickRecID  +"' ";
                DataRow drCheck = mdbcon.GetDataFirstRow(strSQL);
                if (drCheck != null)
                    chkSickHistory.Checked = true;
                else
                    chkSickHistory.Checked = false;
            }

           
        }

        private bool FillGrid()
        {
            //if (mstrRecID == "" || mstrRecID == "0") return true;
            string strSQL = string.Empty;
            string strTmpValue = "";
            strSQL = "select * from "+ mstrTableName  +" where SeqNo='"+ mstrSeqNo +"' and SickRecID='"+ mstrSickRecID +"' ";
            DataRow drDetail = mdbcon.GetDataFirstRow(strSQL);
            if (drDetail != null)
            {
                txtTreatment.Text = CvtFunc.ToString(drDetail["Treatment"]);
                txtPhysicalSign_2.Text = CvtFunc.ToString(drDetail["physicalsign"]);
                txtDiagnosis.Text = CvtFunc.ToString(drDetail["Diagnosis"]);
                
                #region 实验室检查
                txtLabExam_1.Text = CvtFunc.ToString(drDetail["LabExam_1"]);
                txtLabExam_2.Text = CvtFunc.ToString(drDetail["LabExam_2"]);
                txtLabExam_3.Text = CvtFunc.ToString(drDetail["LabExam_3"]);
                txtLabExam_4.Text = CvtFunc.ToString(drDetail["LabExam_4"]);
                txtLabExam_5.Text = CvtFunc.ToString(drDetail["LabExam_5"]);
                txtLabExam_6.Text = CvtFunc.ToString(drDetail["LabExam_6"]);
                txtLabExam_7.Text = CvtFunc.ToString(drDetail["LabExam_7"]);
                #endregion

                txtMainSickpersonTell.Text = CvtFunc.ToString(drDetail["MainSickpersonTell"]);
                udtTreatDate.Value = BaseFunctions.FormatDate(CvtFunc.ToString(drDetail["TreatDate"]), DateFormatOption.DateFmtYMD);
                txtNextVisit.Text = CvtFunc.ToString(drDetail["NextVisit"]);
                txtUserDrug.Text = CvtFunc.ToString(drDetail["UserDrug"]);
                txtTreatment.Text = CvtFunc.ToString(drDetail["Treatment"]);
                udtNextTreatDate.Value = CvtFunc.ToDateString(drDetail["NextTreatDate"]);

                #region 治疗方案

                strTmpValue = CvtFunc.ToString(drDetail["Case_1"]);
                if (strTmpValue != "") chkCase_1.Checked = true;

                strTmpValue = CvtFunc.ToString(drDetail["Case_2"]);
                if (strTmpValue != "") chkCase_2.Checked = true;

                strTmpValue = CvtFunc.ToString(drDetail["Case_3"]);
                if (strTmpValue != "") chkCase_3.Checked = true;

                strTmpValue = CvtFunc.ToString(drDetail["Case_4"]);
                if (strTmpValue != "") chkCase_4.Checked = true;

                strTmpValue = CvtFunc.ToString(drDetail["Case_5"]);
                if (strTmpValue != "") chkCase_5.Checked = true;

                #endregion

                #region 体征
                //strTmpValue = CvtFunc.ToString(drDetail["PhysicalSign_1"]).Trim();
                //if (strTmpValue != "") chkPhysicalSign_1.Checked = true;


                //strTmpValue = CvtFunc.ToString(drDetail["PhysicalSign_2"]).Trim();
                //if (strTmpValue != "") chkPhysicalSign_2.Checked = true;


                //strTmpValue = CvtFunc.ToString(drDetail["PhysicalSign_3"]).Trim();
                //if (strTmpValue != "") chkPhysicalSign_3.Checked = true;


                //strTmpValue = CvtFunc.ToString(drDetail["PhysicalSign_4"]).Trim();
                //if (strTmpValue != "") chkPhysicalSign_4.Checked = true;


                //strTmpValue = CvtFunc.ToString(drDetail["PhysicalSign_5"]).Trim();
                //if (strTmpValue != "")
                //{
                //    chkPhysicalSign_5.Checked = true;
                //    PhysicalSign_2.Text = CvtFunc.ToString(drDetail["txtPhysicalSign_5"]);
                //}

                txtPhysicalSign_1.Text = drDetail["PhysicalSign_1"].ToString().Trim();
                txtPhysicalSign_2.Text = drDetail["PhysicalSign_2"].ToString().Trim();
                txtPhysicalSign_3.Text = drDetail["PhysicalSign_3"].ToString().Trim();
                txtPhysicalSign_4.Text = drDetail["PhysicalSign_4"].ToString().Trim();
                txtPhysicalSign_5.Text = drDetail["PhysicalSign_5"].ToString().Trim();
                 
                #endregion

                #region 具体治疗方法
                txtTreatment_1.Text = CvtFunc.ToString(drDetail["Treatment_1"]).Trim();
                txtTreatment_2.Text = CvtFunc.ToString(drDetail["Treatment_2"]).Trim();
                txtTreatment_3.Text = CvtFunc.ToString(drDetail["Treatment_3"]).Trim();
                txtTreatment_4.Text = CvtFunc.ToString(drDetail["Treatment_4"]).Trim();
                txtTreatment_5.Text = CvtFunc.ToString(drDetail["Treatment_5"]).Trim();
                #endregion

                txtViewRange.Text = CvtFunc.ToString(drDetail["ViewRange"]);

                txtTreatmentHistory.Text = CvtFunc.ToString(drDetail["TreatmentHistory"]);
            }
            else
            {
                txtTreatment.Text = "";
            }

            FillVisit();

            return true;

        }

        private bool ValidCheck()
        {
            string strErrMsg = string.Empty;
            if ( !BaseFunctions.IsDate (  udtTreatDate.Value))
            {
                strErrMsg = "日期不能为空.";
            }
            if (strErrMsg != "")
            {
                MessageBox.Show(strErrMsg, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else
                return true;
        }

        private bool UpdateDetail()
        {
            string strSQL = string.Empty;
            DataRow drRow = null;
            string strSeqNo = string.Empty;
            if (!ValidCheck()) return false;
            try
            {
                mdbcon.BeginTran();
                if (mfrmStatus == FormStatus.Update)
                {
                    strSQL = "select top 1 * from " + mstrTableName + " where SeqNo='" + mstrSeqNo + "' and SickRecID='" + mstrSickRecID + "' ";
                    drRow = mdbcon.GetDataFirstRow(strSQL);
                    strSeqNo = CvtFunc.ToString(drRow["SeqNo"]);
                }
                else if (mfrmStatus == FormStatus.AddNew)
                {
                    drRow = mdbcon.GetDataTable("select top 0 * from " + mstrTableName + " ").NewRow();
                    strSeqNo = (CvtFunc.ToInt(DataFunctions.GetFieldValue(mstrTableName, "max(SeqNo) as SeqNo", " 1=1 and SickRecID='" + mstrSickRecID + "' ")) + 1).ToString();
                }

                drRow["SeqNo"] = strSeqNo;
                drRow["SickRecID"] = mstrSickRecID;
                drRow["SickName"] = txtSickName.Text;
                drRow["Treatment"] = CvtFunc.ToString(txtTreatment.Text);
                drRow["physicalsign"] = CvtFunc.ToString(txtPhysicalSign_2.Text);
                drRow["Diagnosis"] = CvtFunc.ToString(txtDiagnosis.Text);
                drRow["LabExam"] = CvtFunc.ToString(txtLabExam_1.Text);
                drRow["MainSickpersonTell"] = CvtFunc.ToString(txtMainSickpersonTell.Text);
                drRow["TreatDate"] = CvtFunc.ToDateTime(udtTreatDate.Value);
                drRow["NextVisit"] = CvtFunc.ToInt(txtNextVisit.Text);
                //drRow["Treatment"] = txtTreatment.Text;
                drRow["UserDrug"] = txtUserDrug.Text;
                drRow["NextTreatDate"] = CvtFunc.ToDateTime(udtNextTreatDate.Value);
                //drRow["CurCaseName"]= DataFunctions.GetFieldValue("mSickPerson","CurOperation"," SickRecID='"+ mstrSickRecID +"'");

                #region 实验室检查
                drRow["LabExam_1"] = txtLabExam_1.Text;
                drRow["LabExam_2"] = txtLabExam_2.Text;
                drRow["LabExam_3"] = txtLabExam_3.Text;
                drRow["LabExam_4"] = txtLabExam_4.Text;
                drRow["LabExam_5"] = txtLabExam_5.Text;
                #endregion


                #region 治疗方案
                string strTmpValue = "";
                strTmpValue = chkCase_1.Text;
                if (chkCase_1.Checked)
                    drRow["Case_1"] = strTmpValue;
                else
                    drRow["Case_1"] = "";

                strTmpValue = chkCase_2.Text;
                if (chkCase_2.Checked)
                    drRow["Case_2"] = strTmpValue;
                else
                    drRow["Case_2"] = "";

                strTmpValue = chkCase_3.Text;
                if (chkCase_3.Checked) 
                    drRow["Case_3"] = strTmpValue;
                else
                    drRow["Case_3"] = "";

                strTmpValue = chkCase_4.Text;
                if (chkCase_4.Checked)
                    drRow["Case_4"] = strTmpValue;
                else
                    drRow["Case_4"] = "";

                strTmpValue = chkCase_5.Text;
                if (chkCase_5.Checked) 
                    drRow["Case_5"] = strTmpValue;
                else 
                    drRow["Case_5"]="";
                #endregion

                #region 体征
                //strTmpValue =chkPhysicalSign_1.Text ;
                //if (chkPhysicalSign_1.Checked)
                //    drRow["PhysicalSign_1"] = strTmpValue;
                //else
                //    drRow["PhysicalSign_1"] = "";

                //strTmpValue = chkPhysicalSign_2.Text;
                //if (chkPhysicalSign_2.Checked) 
                //    drRow["PhysicalSign_2"] = strTmpValue;
                //else
                //    drRow["PhysicalSign_2"] = "";

                //strTmpValue = chkPhysicalSign_3.Text;
                //if (chkPhysicalSign_3.Checked) 
                //    drRow["PhysicalSign_3"] = strTmpValue;
                //else
                //    drRow["PhysicalSign_3"] = "";

                //strTmpValue = chkPhysicalSign_4.Text;
                //if (chkPhysicalSign_4.Checked) 
                //    drRow["PhysicalSign_4"] = strTmpValue;
                //else
                //    drRow["PhysicalSign_4"] = "";

                //strTmpValue = chkPhysicalSign_5.Text;
                //if (chkPhysicalSign_5.Checked) 
                //    drRow["PhysicalSign_5"] = strTmpValue;
                //else
                //    drRow["PhysicalSign_5"] = "";

                //strTmpValue = txtPhysicalSign_2.Text.Trim();                
                //drRow ["txtPhysicalSign_5"] = strTmpValue;                
                drRow["PhysicalSign_1"] = txtPhysicalSign_1.Text;
                drRow["PhysicalSign_2"] = txtPhysicalSign_2.Text;
                drRow["PhysicalSign_3"] = txtPhysicalSign_3.Text;
                drRow["PhysicalSign_4"] = txtPhysicalSign_4.Text;
                drRow["PhysicalSign_5"] = txtPhysicalSign_5.Text;
                #endregion

                #region 具体治疗方法
                drRow["Treatment_1"] = txtTreatment_1.Text ;
                drRow["Treatment_2"] = txtTreatment_2.Text;
                drRow["Treatment_3"] = txtTreatment_3.Text;
                drRow["Treatment_4"] = txtTreatment_4.Text;
                drRow["Treatment_5"] = txtTreatment_5.Text;
                #endregion

                drRow["ViewRange"] = txtViewRange.Text;
                drRow["TreatmentHistory"] = txtTreatmentHistory.Text;
                string strFields = @" SeqNo,SickRecID,SickName,TreatDate,Treatment,physicalsign,Diagnosis,LabExam,MainSickpersonTell,NextVisit,UserDrug,NextTreatDate,
                                      LabExam_1,LabExam_2,LabExam_3,LabExam_4,LabExam_5,Case_1,Case_2,Case_3,Case_4,Case_5,TreatmentHistory,
                                      PhysicalSign_1,PhysicalSign_2,PhysicalSign_3,PhysicalSign_4,PhysicalSign_5, txtPhysicalSign_5,
                                      Treatment_1,Treatment_2,Treatment_3,Treatment_4,Treatment_5,ViewRange
                                    ";
                if (mfrmStatus == FormStatus.AddNew)
                    DataFunctions.UpdateTable(mdbcon, strFields, mstrTableName, drRow, FormStatus.AddNew);
                else
                    DataFunctions.UpdateTable(mdbcon, strFields, mstrTableName, drRow, FormStatus.Update);


                if (!frmVisit.UpdateDetail(mdbcon))
                {
                    mdbcon.RollbackTran(); 
                    return false;
                }


                if (mfrmStatus == FormStatus.AddNew)
                {
                    string strCurCaseSeqNo = DataFunctions.GetFieldValue("NoseVisitWeek", "CurCaseSeqNo", "SickRecID='" + mstrSickRecID + "'");
                    strSQL = "update NoseVisitWeek set AlreadyVisit" + strCurCaseSeqNo + " =AlreadyVisit" + strCurCaseSeqNo + " +1 where SickRecID='" + mstrSickRecID + "' ";
                    mdbcon.ExecuteSql(strSQL);
                }
                mdbcon.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                mdbcon.RollbackTran(); 
                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;
        }

        private void frmNSDrTreatHistoryDetail_Load(object sender, EventArgs e)
        {
            if (mfrmStatus == FormStatus.AddNew)
            {
                string strCurCaseSeqNo = DataFunctions.GetFieldValue("NoseVisitWeek", "CurCaseSeqNo", "SickRecID='" + mstrSickRecID + "'");
                if (strCurCaseSeqNo == "")
                {
                    MessageBox.Show("请先为此患者的选择治疗方案.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    mblnExit = true;
                    this.Close();

                }
            }
            initContext();
            FillGrid();
            timer1.Start();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            mblnExit = true;
            if (!UpdateDetail()) return ;
 
            this.Close ();
        }

        private void frmNSDrTreatHistoryDetail_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (mblnExit == false)
            {
                if (this.Opacity >= 1)
                {
                    timer1.Stop();
                }
                this.Opacity += 0.04;
            }
            else
            {
                this.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            mblnExit = true;
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (!UpdateDetail()) return; 
            }
            this.Close();
        }

        private void btnSickInfo_Click(object sender, EventArgs e)
        {
            Type tpType;
            object[] args = new object[1];
            args[0] = mstrSickRecID;
            tpType = Type.GetType("YT.XH.MF" + ".frmSickPersonMtn," + "YT.XH.MF", false, true);
            if (tpType == null) return;
            Form frmForm = (Form)System.Activator.CreateInstance(tpType,args);
            frmForm.StartPosition = FormStartPosition.CenterParent;
            frmForm.ShowDialog();

        }

        private void btnSickHistory_Click(object sender, EventArgs e)
        {
            Form frmForm = new frmNSSickHistory(mstrSickRecID); 
            frmForm.StartPosition = FormStartPosition.CenterParent;
            frmForm.ShowDialog();
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void btnPS_Click(object sender, EventArgs e)
        {
            frmNSSkinPrickTestHdr frmHdr = new frmNSSkinPrickTestHdr();
            frmHdr.ShowDialog(); 
        }

        private void frmNSDrTreatHistoryDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!mblnExit)
            {
                if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (!UpdateDetail()) e.Cancel =true ;
                }
            }

        }

        private void txtNextVisit_TextChanged(object sender, EventArgs e)
        {
            if (mblnPressNextVist)
            {
                udtNextTreatDate.Value = CvtFunc.ToDateString ( DateTime.Today.AddDays(CvtFunc.ToInt(txtNextVisit.Text) * 7)); 
            }
        }

        private void txtNextVisit_KeyPress(object sender, KeyPressEventArgs e)
        {
            mblnPressNextVist = true;
        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void txtphysicalsign_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox12_TextChanged(object sender, EventArgs e)
        {

        }

        private void PhysicalSign_5_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
